﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Sebrae.Academico.Trilhas.BP;
using Sebra.Academico.InfraEstrutura.Core.Helper;

namespace Sebrae.Academico.Trilhas.WebForms.Cadastros
{

    public partial class ListaTrilhaNivel : System.Web.UI.Page
    {

        private ManterTrilhaNivel trilhaNivel = new ManterTrilhaNivel();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack) return;


            DataTable tbTrilhaNive = DataHelper.ConvertToDataTable(trilhaNivel.ObterTodos());

            CarregaGrid(trilhaNivel.ObterTodos());
            


            DataTable tbPreRequisitos = new DataTable("tbPreRequisito");
            tbPreRequisitos.Columns.Add(new DataColumn("ID", typeof(int)));
            tbPreRequisitos.Columns.Add(new DataColumn("Nome", typeof(string)));

            DataRow dr = tbPreRequisitos.NewRow();
            dr["ID"] = -1;
            dr["Nome"] = "- Todos -";
            tbPreRequisitos.Rows.Add(dr);

            foreach (Sebrae.Academico.Trilhas.Dominio.Classes.TrilhaNivel pr in trilhaNivel.ObterTrilhaNivelPreRequisito())
            {
                dr = tbPreRequisitos.NewRow();
                dr["ID"] = pr.ID;
                dr["Nome"] = pr.Nome;
                tbPreRequisitos.Rows.Add(dr);
            }

            ddlTrilhaNivelPrerequisito.DataSource = tbPreRequisitos;
            ddlTrilhaNivelPrerequisito.DataMember = tbPreRequisitos.TableName;
            ddlTrilhaNivelPrerequisito.DataTextField = "Nome";
            ddlTrilhaNivelPrerequisito.DataValueField = "ID";
            ddlTrilhaNivelPrerequisito.DataBind();
            txtNome.Text = "";




        }

        private void CarregaGrid(IList<Dominio.Classes.TrilhaNivel> pLista)
        {
            DataTable tbTrilhaNivel = new DataTable("tbTrilhaNivel");
            tbTrilhaNivel.Columns.Add(new DataColumn("ID", typeof(int)));
            tbTrilhaNivel.Columns.Add(new DataColumn("PreRequisito", typeof(string)));
            tbTrilhaNivel.Columns.Add(new DataColumn("Nome", typeof(string)));
            tbTrilhaNivel.Columns.Add(new DataColumn("QtdPrazoDias", typeof(int)));

            foreach (Sebrae.Academico.Trilhas.Dominio.Classes.TrilhaNivel tn in pLista)
            {
                DataRow dr = tbTrilhaNivel.NewRow();
                dr["ID"] = tn.ID;
                if (tn.PreRequisito != null)
                {
                    dr["PreRequisito"] = tn.PreRequisito.Nome;
                }
                dr["Nome"] = tn.Nome;
                dr["QtdPrazoDias"] = tn.QuantidadeDiasPrazo;
                tbTrilhaNivel.Rows.Add(dr);
            }

            tbTrilhaNivel = DataHelper.OrdenarLista(tbTrilhaNivel, "PreRequisito");
            Session.Add("listaGrid", tbTrilhaNivel.Copy());

            dgTrilhaNivel.DataSource = tbTrilhaNivel;
            dgTrilhaNivel.DataMember = tbTrilhaNivel.TableName;
            dgTrilhaNivel.DataBind();
        }

        

        protected void dgTrilhaNivel_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            trilhaNivel.Excluir(new Sebrae.Academico.Trilhas.Dominio.Classes.TrilhaNivel() { ID = int.Parse(e.Keys[0].ToString()), Nome = "delete" });
            Response.Redirect("ListaTrilhaNivel.aspx");
        }

        protected void dgTrilhaNivel_SelectedIndexChanged(object sender, EventArgs e)
        {
            Session.Add("trilhaSelect", dgTrilhaNivel.DataKeys[dgTrilhaNivel.SelectedIndex].Value);
            Response.Redirect("EdicaoTrilhaNivel.aspx", false);
        }

        protected void btnNovo_Click(object sender, EventArgs e)
        {
            Session.Remove("trilhaSelect");
            Response.Redirect("EdicaoTrilhaNivel.aspx", false);
        }

        protected void dgTrilhaNivel_Sorting(object sender, GridViewSortEventArgs e)
        {
            DataTable dtsort = DataHelper.OrdenarLista((DataTable)Session["listaGrid"],e.SortExpression);
            dgTrilhaNivel.DataSource = dtsort;
            dgTrilhaNivel.DataBind();
            Session.Add("listaGrid", dtsort.Copy());

        }

        protected void btnPesquisar_Click(object sender, EventArgs e)
        {
            IList<Dominio.Classes.TrilhaNivel> lstFiltro = trilhaNivel.ObterDadosPesquisa(txtNome.Text, int.Parse(ddlTrilhaNivelPrerequisito.SelectedValue), int.Parse((txtPrazo.Text.Length > 0?txtPrazo.Text:"0")));

            CarregaGrid(lstFiltro);

        }
    }
}